package com.xtaller.sdboot.config.interceptor;

import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : Taller
 * @date : 2020/2/28 00:28
 * @Description : 基础拦截
 */
@Slf4j
@Component
public class BasicFilter extends OriginalFilter {


    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        // 排除验证的路由规则
        if (checkRequestUrl(request)){
            return true;
        }
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        var pm = getByRequest(request);
        if(pm.getClassName() == null){
            response.setStatus(403);
            response.getWriter().write(result(403, "禁止访问"));
            return false;
        }
        if(pm == null){
            response.setStatus(401);
            response.getWriter().write(result(401, "请重新登录"));
            return false;
        }
        if(pm.getIsCommon()){
            // 404判断
            if(pm.getClassName() == null){
                response.setStatus(404);
                response.getWriter().write(result(404, "请检查Api路由是否正确"));
                return false;
            }
        }
        // 进入下一层
        return true;
    }



}
